package cos.android.tarificador;

import java.sql.SQLException;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cos.android.tarificador.databasehandler.Constants;
import cos.android.tarificador.entities.Kind;

public class KindActivity extends ListActivity {

	public static class IconAdapter extends BaseAdapter {
		LayoutInflater mInflater;
		int size;
		ArrayList<Kind> kindList;
		private Bitmap mIcon1;
        private Bitmap mIcon2;

		private IconAdapter(Context context, ArrayList<Kind> kindList) {
			// Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);
            this.kindList = kindList;
			size = kindList.size();
			mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.iconred);
			mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.iconyellow);
			
		}

		@Override
		public int getCount() {
			return size;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			ViewHolder holder;
			
			if(convertView==null){
				
				convertView = mInflater.inflate(R.layout.row_list, null);
				
				holder = new ViewHolder();
				holder.text=(TextView) convertView.findViewById(R.id.text);
				holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
				
			}else{
				holder =(ViewHolder)convertView.getTag();
			}
			holder.text.setText(kindList.get(position).getKindName());
			String colour = kindList.get(position).getColour();
			if (colour.equals("red")){
				holder.icon.setImageBitmap(mIcon1);	
			}else{
				holder.icon.setImageBitmap(mIcon2);	

			}
		
			return convertView;
		}
		
		static class ViewHolder {
            TextView text;
            ImageView icon;
        }

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		addKind();
		Cursor c = getKinds();
		ArrayList<Kind> kindList = cursorToKindList(c);
		setListAdapter(new IconAdapter(this, kindList));

		
	}
	
	public void addKind(){
		String[] aux = {"Kind1"};
    	try{
		getContentResolver().delete(Constants.CONTENT_URI_KIND, Constants.KINDNAME, aux);
    	}catch(Exception e){}
    	ContentValues values = new ContentValues();
    	values.put(Constants.KINDNAME, "Kind1");
    	values.put(Constants.COLORKIND, "yellow");
    	getContentResolver().insert(Constants.CONTENT_URI_KIND, values);
	}
	
	public Cursor getKinds(){
		String [] from ={Constants._IDKIND, Constants.KINDNAME, Constants.COLORKIND};
		return managedQuery(Constants.CONTENT_URI_KIND, from, null, null, "");	
	}
	
	public ArrayList<Kind> cursorToKindList(Cursor c){
		
		ArrayList<Kind> result = new ArrayList<Kind>();
		
		if(c.moveToFirst()){
			Kind kind;		
			do{
				kind = new Kind();
				kind.setKindId(c.getInt(c.getColumnIndex(Constants._IDKIND)));
				kind.setKindName(c.getString(c.getColumnIndex(Constants.KINDNAME)));
				kind.setColour(c.getString(c.getColumnIndex(Constants.COLORKIND)));
				result.add(kind);
				
			}while(c.moveToNext());	
		}
		return result;
		
	}

}
